package com.stu.tools;


import com.stu.controller.ExcelController;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

public class MyTools {
    public boolean typeTest(String str, String type) {
        boolean flag = false;
        String telnum = "(^((13[0-9])|(14[5-8])|(15([0-3]|[5-9]))|(16[6])|(17[0|4|6|7|8])|(18[0-9])|(19[8-9]))\\d{8}$)|(^((170[0|5])|(174[0|1]))\\d{7}$)|(^(14[1|4])\\d{10}$)";
        String emailAddress = "[a-zA-Z0-9_\\.-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z0-9]{2,4}";
        String faxNum = "^\\d{8,}$";
        String bankAccout = "([3-6]{1})([0-9]{15})([0-9]{3})";
        switch (type) {
            case "tel":
                flag = Pattern.matches(telnum, str);
                break;
            case "email":
                flag = Pattern.matches(emailAddress, str);
                break;
            case "fax":
                flag = Pattern.matches(faxNum, str);
                break;
            case "bank":
                flag = Pattern.matches(bankAccout, str);
                break;
        }
        return flag;
    }

    public Integer turnGetState(String state) {
        if (state.equals("可用")) {
            return 1;
        }
        return 0;
    }

    public Integer turnSex(String sex) {
        if (sex.equals("男") || sex.equals("nan") || sex.equals("1")) {
            return 1;
        } else if (sex.equals("女") || sex.equals("nv") || sex.equals("0")) {
            return 0;
        }
        return null;
    }

    public Date turnDate(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(time);
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }


    public BigDecimal turnPrice(String price) {
        BigDecimal bg = new BigDecimal(price);
        return bg;
    }

    public double turnDoublePrice(BigDecimal price) {

        double latitudeDou = price.doubleValue();
        return latitudeDou;
    }

    public BigDecimal turnDoubleToDec(double price) {
        BigDecimal bg = new BigDecimal(price);
        return bg;
    }

    public String getDateTime() {
        Date creat = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(creat);
        return time;
    }

    public String getPath(String path) {
        URL url = ExcelController.class.getClassLoader().getResource("");
        try {
            File file = new File(url.getFile() + "/static" + path);
            if (!file.exists()) {
                //创建文件夹
                file.mkdir();
            }
            return URLDecoder.decode(url.getFile() + "static" + path, "utf8");
        } catch (UnsupportedEncodingException e) {
            return url.getFile();
        }
    }

    public static void delFile(String newFilePath) {
        File oldFile = new File(newFilePath);
        if (oldFile.exists() && oldFile.isFile()) {
            oldFile.delete();
        }
    }

    public static String getStatus(int state) {
        String status = null;
        switch (state) {
            case 0:
                status = "在校";
                break;
            case 1:
                status = "休学";
                break;
            case 2:
                status = "辍学";
                break;
        }
        return status;
    }

//    public static String imgToBase64(MultipartFile uploadFiles) {
//        InputStream in;
//        byte[] data = null;
//        //读取图片字节数组
//        try {
//            byte[] byteArr = uploadFiles.getBytes();
//            in = new ByteArrayInputStream(byteArr);
//            data = new byte[in.available()];
//            in.read(data);
//            in.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (Exception e1) {
//            e1.getMessage();
//            e1.printStackTrace();
//        }
//        return new String(Objects.requireNonNull(Base64.encodeBase64(data)));
//
//    }
}